<?php
namespace app\common\order\model;

use app\common\config\logic\DictionaryLogic;
use app\common\customer\model\Customer;
use app\common\customer\model\CustomerAllocate;
use app\common\customer\model\CustomerInvite;
use app\common\system\logic\UserLogic;
use think\db\exception\DataNotFoundException;
use think\Model;
use think\model\concern\SoftDelete;

class Order extends Model
{
    use SoftDelete;
    protected $defaultSoftDelete = 0;
    

    protected $type = [
        'files' => 'json',
        'images' => 'json',
        'sign_date' => 'timestamp',
        'event_date' => 'timestamp'
    ];

    public function member()
    {
        return $this->hasOne(Customer::class, "id", "member_id");
    }

    public function getSourceAttr($value, $data) {
        if(empty($data['source_id'])) return [
            'id' => '',
            'title' => ''
        ];

        $logic = new DictionaryLogic();
        return $logic->getFullPath($data['source_id']);
    }

    public function getCustomerAttr($value, $data) 
    {
        $customerModel = new Customer();
        $fields = "id,fullname,create_id,source_id,status,create_time";
        try {
            $customer = $customerModel->where('id', $data['member_id'])->field($fields)->find();
            $customer->append(['creator', 'source', 'visit_status', 'contact']);
                
        } catch (DataNotFoundException $e) {
            $customer = [
                'id' => '',
                'fullname' => '',
                'creator' => [
                    'id' => '',
                    'realname' => '',
                ],
                'source' => [
                    'id' => '',
                    'title' => ''
                ],
                'visit_status' => [
                    'id' => '',
                    'title' => ''
                ],
                'contact' => [
                    'id' => '',
                    'contact' => ''
                ],
                'create_time' => ''
            ];
        }

        return $customer;
    }

    public function getAllocateAttr($value, $data) {
        if ($data['allocate_id'] > 0) {
            $allocateModel = new CustomerAllocate();
            $allocate = $allocateModel::withTrashed()->where('id', $data['allocate_id'])->field('id,user_id,member_id,status,create_time')->find();
            $allocate->append(['user', 'contact', 'visit_status']);
        } else {
            $allocate = [
                'id' => '',
                'user_id' => '',
                'user' => [
                    'id' => '',
                    'realname' => ''
                ],
                'contact' => [
                    'id' => '',
                    'contact' => ''
                ],
                'visit_status' => [
                    'id' => '',
                    'title' => ''
                ],
                'create_time' => ''
            ];
        }

        return $allocate;
    }

    // 获取到店信息
    public function getInviteAttr($value, $data) {
        $model = new CustomerInvite();
        $invite = $model->where('member_id', $data['member_id'])->order('id desc')->find();
        if ($invite) {
            $invite->append(['store','status_text']);
        } else {
            $invite = [
                'id' => '',
                'status_text' => '',
                'store' => [
                    'id' => '',
                    'title' => ''
                ],
                'invite_time' => '',
            ];
        }

        return $invite;
    }

    public function getInvitesAttr($value, $data) {
        $customerInviteModel = new CustomerInvite();
        $rows = $customerInviteModel->where('member_id', $data['member_id'])->order('id asc')->select();
        $invite = [
            'store' => '',
            'first_visit' => [
                'visit_time' => '',
                'remark' => '',
            ],
            'second_visit' => [
                'visit_time' => '',
                'remark' => '',
            ],
            'third_visit' => [
                'visit_time' => '',
                'remark' => '',
            ],
        ];

        if ($rows->isEmpty()) return $invite;

 
        $row = $rows->first();
        // 第一次邀约
        $invite['store'] = '';
        $invite['first_visit']['visit_time'] = $row->visit_time;
        $invite['first_visit']['remark'] = $row->remark;

        // 移到下一个
        $rows->shift();

        $row = $rows->first();
        if ($rows->isEmpty()) {
            // 第二次邀约
            $invite['second_visit']['visit_time'] = '';
            $invite['second_visit']['remark'] = '';
        } else {
            // 第二次邀约
            $invite['second_visit']['visit_time'] = $row->visit_time;
            $invite['second_visit']['remark'] = $row->remark;
        }

        // 移到下一个
        $rows->shift();

        $row = $rows->first();
        if ($rows->isEmpty()) {
            // 第三次邀约
            $invite['third_visit']['visit_time'] = '';
            $invite['third_visit']['remark'] = '';
        } else {
            // 第三次邀约
            $invite['third_visit']['visit_time'] = $row->visit_time;
            $invite['third_visit']['remark'] = $row->remark;
        }

        return $invite;
    }
    

    public function getUserAttr($value, $data) {
        $userLogic = new UserLogic();
        return $userLogic->getUser($data['user_id'], ['id', 'realname', 'work_wechat']);
    }

    // order vip
    public function getVipAttr($value, $data) {
        $model = new OrderVip();
        $vip = $model->where('order_id', $data['id'])->hidden(['create_time', 'update_time', 'delete_time'])->find();

        return $vip;
    }

    // order bom
    public function getBomAttr($value, $data) {
        $model = new OrderBom();
        $bom = $model->where('order_id', $data['id'])->hidden(['create_time', 'update_time', 'delete_time'])->find();

        $bom->append(['store']);

        return $bom;
    }

    // 获取已收总额
    public function getIncomeTotalsAttr($value, $data) {
        $model = new OrderIncome();
        $total = $model->where('order_id', $data['id'])->sum('total');

        return $total;
    }
}